﻿//********************************************************************************//
//创建日期：<创建日期，2016-08-16>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：一般管理水表信息查询
//********************************************************************************//
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Data;

namespace Web.Pages.YongShui._12.ChaXunGL
{
    public partial class ShuiBiaoXXCX_YB : System.Web.UI.Page
    {
        #region 属性

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtYueChaoBQKTab1
        {
            get
            {
                return ViewState["dtYueChaoBQKTab1"] as DataTable;
            }
            set
            {
                ViewState["dtYueChaoBQKTab1"] = value;
            }
        }
        #endregion

        #region 变量

        /// <summary>
        /// 行政区bll
        /// </summary>
        YS_JX_XINGZHENGQBLL _YS_JX_XINGZHENGQBLL = new YS_JX_XINGZHENGQBLL();
        /// <summary>
        /// 配置表bll
        /// </summary>
        S_WORDSBLL _S_WORDSBLL = new S_WORDSBLL();

        /// <summary>
        /// 考核水量
        /// </summary>
        YS_CX_KAOHESL _YS_CX_KAOHESL = new YS_CX_KAOHESL();

        /// <summary>
        /// 考核水量BLL
        /// </summary>
        YS_CX_KAOHESLBLL _YS_CX_KAOHESLBLL = new YS_CX_KAOHESLBLL();
        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            
            gvYueChaoBQKTab1.DataSource = this.dtYueChaoBQKTab1;
            gvYueChaoBQKTab1.DataBind();
        }

        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXunTab1_Click(object sender, EventArgs e)
        {
            LoadGridDataTab1();
            if (this.dtYueChaoBQKTab1 == null || this.dtYueChaoBQKTab1.Rows.Count < 1)
            {
                txtShuiBiaoH.Text = "";
                txtXingZhengQK.Text = "";
                txtSheShuiDXDM.Text = "";
                txtSheShuiDXMC.Text = "";
                txtChaoBiaoRQ.Text = "";
                txtBiaoFenL.Text = "";
                txtShuiBiaoHM2.Text = "";
                txtChaoMa.Text = "";
                txtYongShuiL.Text = "";
                txtShuiBiaoDZ2.Text = "";
                txtChaoBiaoZT.Text = "";
                txtShuiSiZT2.Text = "";
                txtKouJing.Text = "";
                txtZongBiaoH.Text = "";
                txtGuanWangKH2.Text = "";
                txtBiaoCeH.Text = "";
                txtZhuangBiaoRQ.Text = "";
                txtYongShuiJ.Text = "";
                zjl.Text = "共0条";
            }
        }

       
        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            gvYueChaoBQKTab1.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            // 加载抄表部门
            LoadCBBM();
            // 加载行政区
            LoadXingZhengQ();
            //水量年月
            LoadShuiLiangYM();
            //涉水对象
            LoadYSXZ_SSDX();
            //抄表状态，水司状态
            LoadWords();
            //获取考核水量的最大水量年月和最小水量年月
            DataTable dt_mym = _SpHelper.P_YSCX_ZLSGSSJCX_KHSL_YM().Tables[0];
           
        }
        /// <summary>
        /// 加载抄表部门
        /// </summary>
        private void LoadCBBM()
        {
            string[] arrCBBM = new string[19] { "市南", "市北", "浦东", "闵行", "虹口", "杨浦", "新黄浦", "卢湾", "长宁", "徐汇", "闸北", "普陀", "普北", "普南", "静安", "黄埔", "宝山", "青浦", "松江" };
            for (int i = 0; i < arrCBBM.Length; i++)
            {
                cmbChaoBiaoBMtab1.Items.Add(arrCBBM[i], i + 2);
            }
        }

        /// <summary>
        /// 加载行政区
        /// </summary>
        private void LoadXingZhengQ()
        {
            DataTable dtXingZhengQ = _YS_JX_XINGZHENGQBLL.SelectYS_JX_XINGZHENGQsDynamic("i_jlzt=0", " s_bianhao");
            for (int i = 0; i < dtXingZhengQ.Rows.Count; i++)
            {
                cmbXingZhengQtab1.Items.Add(dtXingZhengQ.Rows[i]["s_bianhao"] + "_" + dtXingZhengQ.Rows[i]["S_MINGCHENG"].ToString(), dtXingZhengQ.Rows[i]["s_bianhao"]);
            }
        }
        /// <summary>
        /// 加载水量年月
        /// </summary>
        private void LoadShuiLiangYM()
        {
            string water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
           
            int i_y = Convert.ToInt32(water_y_m) / 100;
            int i_m = Convert.ToInt32(water_y_m) % 100;
            for (int i = i_y - 5; i <= i_y + 5; i++)
            {
                cmbShuiLiangYtab1.Items.Add(i.ToString());
            }
            cmbShuiLiangYtab1.SelectedIndex = 5;
           
            for (int i = 1; i <= 12; i++)
            {
                cmbShuiLiangMtab1.Items.Add(i.ToString());
            }
            cmbShuiLiangMtab1.SelectedIndex = i_m - 1;
        }

        /// <summary>
        /// 加载涉水对象
        /// </summary>
        private void LoadYSXZ_SSDX()
        {
            //涉水对象
            var dt_ssdx = _SpHelper.P_YSCX_YSXZ_SSDX(2).Tables[0];
            for (var i = 0; i < dt_ssdx.Rows.Count; i++)
            {
                cmbSheShuiDXFL_h1.Items.Add(dt_ssdx.Rows[i]["XH"] + "_" + dt_ssdx.Rows[i]["NA"], dt_ssdx.Rows[i]["XH"]);
            }
            //行业分类
            var dt_hhfl = _SpHelper.P_YSCX_YSXZ_SSDX(3).Tables[0];
            for (var i = 0; i < dt_hhfl.Rows.Count; i++)
            {
                cmbSheShuiDXFL_h2.Items.Add(dt_hhfl.Rows[i]["XH"] + "_" + dt_hhfl.Rows[i]["NA"], dt_hhfl.Rows[i]["XH"]);
            }
        }

        /// <summary>
        /// 加载抄表状态，水司状态
        /// </summary>
        private void LoadWords()
        {
            DataTable dtShuiBiao = _S_WORDSBLL.SelectS_WORDsDynamic("belongid IN (6,9) ", "BELONGID,wordsid");
            for (int i = 0; i < dtShuiBiao.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtShuiBiao.Rows[i]["BELONGID"]) == 9)
                {
                    cbmChaoBiaoZT.Items.Add(dtShuiBiao.Rows[i]["WORDSID"] + "_" + dtShuiBiao.Rows[i]["WORDSCONTENT"].ToString(), dtShuiBiao.Rows[i]["WORDSID"]);
                }
            }
            //20190411 改为文本框
            //string[] arrSSZT = new string[] { "G1停走", "G2失灵", "G3黑面", "G4壳漏", "G5玻璃", "G6倒装", "G7针偏", "G8其他", "H1推算估计", "H2新表实发", "H3推算正常", "H4实发估计", "J0空屋", "J1新装", "J2多估", "J3未拆" };
            //for (int i = 0; i < arrSSZT.Length; i++)
            //{
            //    cbmShuiSiZT.Items.Add(arrSSZT[i]);
            //}
        }


        /// <summary>
        /// 加载查询数据
        /// </summary>
        private void LoadGridDataTab1()
        {
            string sCbbm = cmbChaoBiaoBMtab1.Text;
            string sXzq = cmbXingZhengQtab1.Value.ToString();
            int i_y = Convert.ToInt32(cmbShuiLiangYtab1.Text);
            int i_m = Convert.ToInt32(cmbShuiLiangMtab1.Text);
            string sKhlx = "0";
            int v_b = -1;
            string constr = "";
            string sYsl = "";
            #region 查询条件
            if (rbnShuiBiaoBH.Checked && txtShuiBiaoBH.Text != "")//水表编号
            {
                v_b = 1;
                constr = txtShuiBiaoBH.Text;
            }
            else if (rbnYongHuH.Checked && txtYongHuH.Text != "")//用户号
            {
                v_b = 2;
                constr = txtYongHuH.Text;
            }
            else if (rbnShuiBiaoHM.Checked && txtShuiBiaoHM.Text != "")//水表户名
            {
                v_b = 3;
                constr = txtShuiBiaoHM.Text;
            }
            else if (rbnShuiBiaoDZ.Checked && txtShuiBiaoDZ.Text != "") //水表地址
            {
                v_b = 4;
                constr = txtShuiBiaoDZ.Text;
            }
            else if (rbnSheShuiDXFL.Checked)//涉水对象分类
            {
                if (cmbSheShuiDXFL_q1.Text != "" && cmbSheShuiDXFL_h1.Text != "")
                {
                    v_b = 5;
                    constr = cmbSheShuiDXFL_q1.Text + cmbSheShuiDXFL_h1.Value.ToString();
                    if (cmbSheShuiDXFL_q2.Text != "" && cmbSheShuiDXFL_h2.Text != "")
                    {
                        constr += " AND  Z.S_DAIMA ";
                        constr += cmbSheShuiDXFL_q2.Text + cmbSheShuiDXFL_h2.Value.ToString();
                    }
                }
            }
            else if (rbnChaoBiaoZT.Checked && cbmChaoBiaoZT.Text != "")//抄表状态
            {
                v_b = 6;
                constr = cbmChaoBiaoZT.Value.ToString();
            }
            else if (rbnShuiSiZT.Checked && txtShuiSiZT.Text != "")//水司状态
            {
                v_b = 7;
                constr = txtShuiSiZT.Text;
            }
            else if (rbnGuanWangKH.Checked && txtGuanWangKH.Text != "")//管网块号
            {
                v_b = 8;
                constr = txtGuanWangKH.Text;
            }
            else if (rbnCeHao.Checked && txtCeHao.Text != "")//册号
            {
                v_b = 9;
                constr = txtCeHao.Text;
            }
            else if (rbnDanJia.Checked && cbmDanJia.Text != "" && txtDanJia.Text != "")//用水价
            {
                v_b = 10;
                constr = cbmDanJia.Text + txtDanJia.Text;
            }
            else if (rbnXuNiB.Checked)//虚拟表
            {
                v_b = 11;
                constr = "";
            }
            #endregion
            if (cbmShuiLiangFW1.Text != "" && txtShuiLiangFW1.Text != "")
            {
                sYsl = " AND A.I_YONGSHUIL" + cbmShuiLiangFW1.Text + txtShuiLiangFW1.Text;
            }
            if (cbmShuiLiangFW2.Text != "" && txtShuiLiangFW2.Text != "")
            {
                sYsl += " AND A.I_YONGSHUIL" + cbmShuiLiangFW2.Text + txtShuiLiangFW2.Text;
            }
            this.dtYueChaoBQKTab1 = _SpHelper.P_YSCX_ZLSGSSJCX_YCBCX_YB(sCbbm, sXzq, i_y, i_m, sKhlx, v_b, constr, sYsl).Tables[0];
            gvYueChaoBQKTab1.DataSource = this.dtYueChaoBQKTab1;
            gvYueChaoBQKTab1.DataBind();
            int zsl = 0;
            for (int i = 0; i < this.dtYueChaoBQKTab1.Rows.Count; i++)
            {
                zsl += Convert.ToInt32(this.dtYueChaoBQKTab1.Rows[i]["I_YONGSHUIL"]);
            }
            txtZongShuiL.Text = zsl.ToString();
            txtShuiBiaoZS.Text = this.dtYueChaoBQKTab1.Rows.Count.ToString();
            if (this.dtYueChaoBQKTab1 != null && this.dtYueChaoBQKTab1.Rows.Count > 0)
            {
                gvYueChaoBQKTab1.FocusedRowIndex = -1;
                gvYueChaoBQKTab1.FocusedRowIndex = 0;
                //SetDataTab1();
            }
            zjl.Text = "共" + this.dtYueChaoBQKTab1.Rows.Count.ToString() + "条";
        }

        
     
       

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            object id = this.hdCollection.Get("S_SHUIBIAOBH");
            string msg = "";
            if (e.Parameter == "setinfo")
            {

                //DataTable dt_temp = _SpHelper.P_YSCX_ZLSGSSJCX_YCBCX("全部", "全部", Convert.ToInt32(cmbShuiLiangYtab1.Text), Convert.ToInt32(cmbShuiLiangMtab1.Text), "全部", 1, id.ToString(), "").Tables[0];
                DataTable dt_temp = _SpHelper.P_YSCX_ZLSGSSJCX_YCBCX_YB("全部", "全部", Convert.ToInt32(cmbShuiLiangYtab1.Text), Convert.ToInt32(cmbShuiLiangMtab1.Text), "0", 1, id.ToString(), "").Tables[0];
                if (dt_temp != null && dt_temp.Rows.Count > 0)
                {
                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_SHUIBIAOBH"]), Convert.ToString(dt_temp.Rows[0]["S_MINGCHENG"]),
                        Convert.ToString(dt_temp.Rows[0]["S_SHESHUIDXFLDM"]), Convert.ToString(dt_temp.Rows[0]["S_SHESHUIDXFLMC"]), Convert.ToString(dt_temp.Rows[0]["D_CHAOBIAORQ"]),
                        Convert.ToString(dt_temp.Rows[0]["I_TYPE"]), Convert.ToString(dt_temp.Rows[0]["S_SHUIBIAOHM"]), Convert.ToString(dt_temp.Rows[0]["I_BENYUECM"]),
                        Convert.ToString(dt_temp.Rows[0]["I_YONGSHUIL"]), Convert.ToString(dt_temp.Rows[0]["S_SHUIBIAODZ"]), Convert.ToString(dt_temp.Rows[0]["CHAOBIAOZT"]),
                        Convert.ToString(dt_temp.Rows[0]["S_YUANCHAOBZT"]), Convert.ToString(dt_temp.Rows[0]["I_KOUJING"]), Convert.ToString(dt_temp.Rows[0]["S_ZBH"]),
                        Convert.ToString(dt_temp.Rows[0]["S_KUAIHAO"]), Convert.ToString(dt_temp.Rows[0]["S_CH"]), Convert.ToString(dt_temp.Rows[0]["D_ZHUANGBIAORQ"]), 
                        Convert.ToString(dt_temp.Rows[0]["N_YSJ"]));
                }

            }
           

            e.Result = e.Parameter + "|0|" + msg;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            this.ASPxGridViewExporter1.GridViewID = "gvYueChaoBQKTab1";
            gvYueChaoBQKTab1.DataSource = dtYueChaoBQKTab1;
            gvYueChaoBQKTab1.DataBind();
            string sName = "水表信息";
            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }
        #endregion
    }
}